<?php
class kcDateTime
{
 protected static
    $months   = array
                 (
                   '1' => array
                             (
                               'long'  => 'Enero',
                               'short' => 'ene',                             
                             ),
                   '2' => array
                             (
                               'long'  => 'Febrero',
                               'short' => 'Feb',                             
                             ),
                   '3' => array
                             (
                               'long'  => 'Marzo',
                               'short' => 'Mar',                             
                             ),
                   '4' => array
                             (
                               'long'  => 'Abril',
                               'short' => 'Abr',                             
                             ),
                   '5' => array
                             (
                               'long'  => 'Mayo',
                               'short' => 'May',                             
                             ),
                   '6' => array
                             (
                               'long'  => 'Junio',
                               'short' => 'Jun',                             
                             ),
                   '7' => array
                             (
                               'long'  => 'Julio',
                               'short' => 'Jul',                             
                             ),
                   '8' => array
                             (
                               'long'  => 'Agosto',
                               'short' => 'Ago',                             
                             ),
                   '9' => array
                             (
                               'long'  => 'Septiembre',
                               'short' => 'Sep',                             
                             ),
                   '10' => array
                             (
                               'long'  => 'Octubre',
                               'short' => 'Oct',                             
                             ),
                   '11' => array
                             (
                               'long'  => 'Noviembre',
                               'short' => 'Nov',                             
                             ),
                   '12' => array
                             (
                               'long'  => 'Diciembre',
                               'short' => 'Dic',                             
                             ),
                 ),
    $days = array
             (
               0 => array
                      (
                        'long'  => 'Domingo',
                        'short' => 'Dom',
                      ),
               1 => array
                      (
                        'long'  => 'Lunes',
                        'short' => 'Lun',
                      ),
               2 => array
                      (
                        'long'  => 'Martes',
                        'short' => 'Mar',
                      ),
               3 => array
                      (
                        'long'  => 'Miercoles',
                        'short' => 'Mie',
                      ),
               4 => array
                      (
                        'long'  => 'Jueves',
                        'short' => 'jue',
                      ),
               5 => array
                      (
                        'long'  => 'Viernes',
                        'short' => 'vie',
                      ),
               6 => array
                      (
                        'long'  => 'Sabado',
                        'short' => 'Sab',
                      ),
             ),
    $ending = array
             (
               1 => 'er',
               2 => 'do',
               3 => 'ro',
               4 => 'to',
               5 => 'to',
               6 => 'to',
               7 => 'mo',
               8 => 'vo',
               9 => 'no',
               0 => 'mo',
             ),
    $day,
    $month,
    $year;  
  private function __construct(){}
    
  public static function generateDays($start = 1, $stop = 31, $length = 'long')
  {
    $results = array();
    for ($i = $start; $i <= $stop; $i++) 
    {
      switch($length)
      {
      	//sprintf('%02d', $i)
        case 'short': $results[$i] = $i; break;//sprintf('%02d', $i); break;
        default     : $results[$i] = $i; break;//sprintf('%02d', $i); break;
      }
    }
    return $results;
  }
    
  public static function generateMonths($start = 1, $stop = 12, $length = 'short')
  {
    $results = array();
    for ($i = $start; $i <= $stop; $i++) 
    {
    	//sprintf('%02d', $i)
      $results[$i] = self::$months[$i][$length];    //sprintf('%02d', $i)][$length];
    }
    return $results;
  }
  public static function generateYears($from = 2004, $to = null)
  {
    $to = $to ? $to : date('Y');
    $years = range($from, $to);
    return array_combine($years, $years);
  }
  
  public static function addCero2Number($number)
  {
    return ($number < 10 ) ? '0'.$number : $number;
  }
  
  public static function splitDate($datetime, $format = 'en', $separator = '-', $type='d')
  {
    $datetime = explode($separator, $datetime);
    switch($type)
    {
      case 'dt' :
        if ($format=='es')
        {
          self::$day   = $datetime[0];
          self::$month = $datetime[1];
          self::$year  = $datetime[2];
        }
        else
        {
          self::$day   = $datetime[2];
          self::$month = $datetime[1];
          self::$year  = $datetime[0];  
        }
        break;
      default:
        if ($format=='es')
        {
          self::$day   = $datetime[0];
          self::$month = $datetime[1];
          self::$year  = substr($datetime[2],0,2);
        }
        else
        {
          self::$day   = substr($datetime[2],0,2);
          self::$month = $datetime[1];
          self::$year  = $datetime[0];  
        }
        break;
    }
  }
                 
  public static function fromDateToString($date, $format = 'en', $separator = '-', $length = 'short', $type='d')
  {
    if (!$date) return '';
    self::splitDate($date, $format, $separator, $type);
    return self::$months[self::$month][$length].' '.self::$day.'<sup>'.self::$ending[substr(self::$day,-1)].'</sup>, '.self::$year;
  }
               
  public static function fromDateToStringDisplayFormat($date, $display_format = 'Y-m-d', $separator = '-', $format = 'en', $length = 'short', $type='d')
  {
    if ($date=='') return '';
    self::splitDate($date, $format, $separator, $type);
  switch($display_format)
  {
    case 'm-d-Y' : 
                 $date = self::$month.$separator.self::$day.$separator.self::$year;
                 break;
    case 'm d Y' : 
                 $date = self::$month.' '.self::$day.' '.self::$year;
                 break;
    case 'M-d-Y' : 
                 $date = self::$months[self::$month][$length].$separator.self::$day.$separator.self::$year;
                 break;
    case 'M-d-y' : 
                 $date = self::$months[self::$month][$length].$separator.self::$day.$separator.substr(self::$year,2,2);
                 break;
    case 'M d Y' : 
                 $date = self::$months[self::$month][$length].' '.self::$day.' '.self::$year;
                 break;
    case 'M, d Y' : 
                 $date = self::$months[self::$month][$length].', '.self::$day.' '.self::$year;
                 break;
    case 'Y m d' : 
                 $date = self::$year.' '.self::$month.' '.self::$day;
                 break;
    case 'Y-M-d' : 
                 $date = self::$year.$separator.self::$months[self::$month][$length].$separator.self::$day;
                 break;
    case 'Y M d' : 
                 $date = self::$year.' '.self::$months[self::$month][$length].' '.self::$day;
                 break;
    case 'Y'     : 
                 $date = self::$year;
                 break;
    case 'M-d' : 
                 $date = self::$months[self::$month][$length].$separator.self::$day;
                 break;               
    default      : 
                 $date = self::$year.$separator.self::$month.$separator.self::$day;
                 break;
  }
    return $date;
  }
  
  public static function getMonthName($month, $length = 'short', $fill_zero = 0)
  {
    if($fill_zero==1)
    {
      self::$month = self::addCero2Number($month);
    }
    return self::$months[self::$month][$length]; 
  }
  
  public static function getDay($day, $length = 'short')
  {
    return self::$days[$day][$length];
  }
  
  public static function getDayNumber($date, $format = 'en', $separator = '-', $type='d')
  {
    if ($date) return '';
    self::splitDate($date, $format, $separator, $type);     
    return jddayofweek ( cal_to_jd(CAL_GREGORIAN, self::$month, self::$day, self::$year) , 0 );
  }
}
?>